<?php

namespace App\Exceptions;

use App\Exceptions\Api\ApiUserNotLoginErrorException;
use App\Exceptions\Front\AddressException;
use App\Exceptions\Interfaces\AddressServiceException;
use App\Exceptions\Interfaces\CommentServiceException;
use App\Exceptions\Interfaces\OrderServiceException;
use App\Exceptions\Interfaces\OrderGoodsServiceException;
use App\Exceptions\Interfaces\UserServiceException;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use PhpParser\Comment;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use App\Exceptions\Interfaces\CartServiceException;
use App\Exceptions\Admin\PermissionDeniedException;
use App\Services\Etouch\EtouchClientException;
use App\Exceptions\Interfaces\ProductServiceException;
use App\Exceptions\Admin\ProductException;

use App\Exceptions\Api\ApiInputErrorException;
use App\Exceptions\Api\ApiValidateErrorException;
use App\Exceptions\Api\ApiDoneErrorException;
use App\Exceptions\Api\ApiRsaEecryptErrorException;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        HttpException::class,
        ModelNotFoundException::class,
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $e
     * @return void
     */
    public function report(Exception $e)
    {
        return parent::report($e);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $e)
    {
        if ($e instanceof ModelNotFoundException) {
            $e = new NotFoundHttpException($e->getMessage(), $e);
        }

        if ($e instanceof AddressException) {

            $return['status'] = 402;
            $return['message'] = $e->getMessage();

            return Response()->json($return);
        }//end if()

        if ($e instanceof AddressServiceException) {

            if ($request->ajax()){
                $return['status'] = 402;
                $return['message'] = $e->getMessage();

                return Response()->json($return);
            }

        }//end if()

        if ($e instanceof OrderServiceException) {

            if ($request->ajax()){
                $return['status'] = 402;
                $return['message'] = $e->getMessage();

                return Response()->json($return);
            }

        }//end if()

        if ($e instanceof OrderGoodsServiceException) {

            if ($request->ajax()){
                $return['status'] = 402;
                $return['message'] = $e->getMessage();
                $return['data'] = $e->getData();

                return Response()->json($return);
            }

        }//end if()

        if ($e instanceof CartServiceException) {

            $return['status'] = 402;
            $return['message'] = $e->getMessage();

            return Response()->json($return);
        }//end if()

        if($e instanceof UserServiceException){
            if ($request->ajax()){

                $return['status'] = 402;
                $return['message'] = $e->getMessage();

                return Response()->json($return);
            }else{

            }

        }

        if($e instanceof CommentServiceException){
            if ($request->ajax()){

                $return['status'] = 402;
                $return['message'] = $e->getMessage();

                return Response()->json($return);
            }else{

            }

        }

        if($e instanceof PermissionDeniedException){

            if ($request->ajax()){

                $return['status'] = 402;
                $return['message'] = $e->getMessage();

                return Response()->json($return);
            }else{

                return Response()->view('admin.permission');
            }

        }
        if($e instanceof EtouchClientException){

            if ($request->ajax()){

                $return['status'] = 402;
                $return['message'] = $e->getMessage();

                return Response()->json($return);
            }

        }

        if($e instanceof ProductServiceException){

            if ($request->ajax()){

                $return['status'] = 402;
                $return['message'] = $e->getMessage();

                return Response()->json($return);
            }

        }
        if($e instanceof ProductException){

            if ($request->ajax()){

                $return['status'] = 402;
                $return['message'] = $e->getMessage();

                return Response()->json($return);
            }

        }



        if ($e instanceof ApiInputErrorException) {

            return response()->json(['message'=>$e->getMessage()],400);
        }

        if ($e instanceof ApiValidateErrorException) {

            return response()->json(['message'=>$e->getMessage()],422);
        }


        if ($e instanceof ApiDoneErrorException) {

            return response()->json(['message'=>$e->getMessage(),'data'=>$e->getData()],202);
        }

        if ($e instanceof ApiRsaEecryptErrorException) {

            return response()->json(['message'=>$e->getMessage()],401);
        }

        if ($e instanceof ApiUserNotLoginErrorException) {

            return response()->json(['message'=>$e->getMessage()],403);
        }


        return parent::render($request, $e);
    }
}
